package com.linrui.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.linrui.gift.client.LoginUserClient;
import com.linrui.gift.domain.Employee;
import com.linrui.gift.domain.LoginUser;
import com.linrui.gift.domain.TenantType;
import com.linrui.gift.dto.TenantDto;
import com.linrui.gift.domain.Tenant;
import com.linrui.gift.exception.BusinessException;
import com.linrui.gift.exception.GiftAssert;
import com.linrui.gift.exception.code.ErrorCode;
import com.linrui.gift.mapper.EmployeeMapper;
import com.linrui.gift.mapper.TenantMapper;
import com.linrui.gift.query.TenantQuery;
import com.linrui.gift.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.linrui.gift.util.AjaxResult;
import com.linrui.gift.util.PageList;
import io.seata.spring.annotation.GlobalLock;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author linrui
 * @since 2022-12-06
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;
    @Override
    public PageList<Tenant> selectPageList(TenantQuery query) {
        //分页条件，查询出来总数
        Page<Tenant> page = new Page<>(query.getPage(), query.getRows());
        //跨表查询出来结构
        List<Tenant> rows  = tenantMapper.loadPageList(page,query);
        return new PageList<>(page.getTotal(),rows);
    }
    @Autowired
    private EmployeeMapper employeeMappermapper;

    @Autowired
    private LoginUserClient loginUserClient;
    @Override
    @GlobalTransactional
    public AjaxResult settlements(TenantDto dto) {
        //参数校验
        Employee admin = dto.getAdmin();
        Tenant tenant = dto.getTenant();
        String password = admin.getPassword();
        String comfirmpassword = admin.getComfirmpassword();
        Integer mealId = dto.getMealId();
        //判断两次密码是否一致
        GiftAssert.assertStrEq(password, comfirmpassword, ErrorCode.ERROR_CODE_TENANT_PASSWORD_NOTEQ);

        //判断店铺是否已经入驻
//        Wrapper<Tenant> wrapper = new EntityWrapper<>();
//        wrapper.eq("company_name", tenant.getCompanyName());  //companyName=XXX
//        List<Tenant> tenants = tenantMapper.selectList(wrapper);
//        GiftAssert.assertListNull(tenants,ErrorCode.ERROR_CODE_TENNAT_EXIST);
        Wrapper<Tenant> wrapper = new EntityWrapper<>();
        wrapper.eq("company_name",tenant.getCompanyName());
        List<Tenant> tenants = tenantMapper.selectList(wrapper);
        GiftAssert.assertListNull(tenants, ErrorCode.ERROR_CODE_ADMIN_EXSIT);

        //判断管理员是否已经注册
        List<Employee> employees = employeeMappermapper.selectList(new EntityWrapper<Employee>().eq("username", admin.getUsername()));
        GiftAssert.assertListNull(employees, ErrorCode.ERROR_CODE_ADMIN_EXSIT);
        //保存管理员信息
        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(admin, loginUser);
        loginUser.setType(0);
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        //保存店铺信息
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());

        admin.setId(loginUser.getId());
        employeeMappermapper.insert(admin);

        tenant.setAdminId(admin.getId());
        tenantMapper.insert(tenant);

        Map<String,Object> params = new HashMap<>();
        params.put("tenantId", tenant.getId());
        params.put("mealId", mealId);
        params.put("expireDate", DateUtils
                .addYears(new Date(), 1));
        params.put("state", 0);
        tenantMapper.saveTenantMeals(params);
        return AjaxResult.me();
    }
}
